<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\Cache;
use app\index\model\traits\ModelHasTime;


class KnowUserQuestion extends Model
{
	use ModelHasTime;

	public function getCourseUserQues($param)
	{
		return self::field('id,content,user_id,answer,time')
					->where('course_id', $param['id'])
					->where('pid', 0)
					->order('time desc')
					->paginate(5,false, ['query'=>$param]);
	}

	public function getCourseUserQuesAnswer($pId)
	{
		return self::field('id,content,user_id,time')
					->where('pid', $pId)
					->order('time desc')
					->select();
	}

	public function getUserQuesIds($param)
	{
		$user = user();

		return self::field('id,content,time')
					->when($param['id'], function($query) use ($param){
						$query->where('course_id', 'in', $param['id']);
					})
					->where('user_id', $user['id'])
					->order('time desc')
					->column('course_id');
	}

	public function getAllData($param, $query = [])
	{
		return self::alias('ques')
					->field('ques.id,know_nickname,know_phone,course_name,nav_name,type_name,s_name,ques.time,answer,pid')
					->when($param['kw'], function($query) use ($param){
						$query->where('content', 'like', '%'.$param['kw'].'%');
					})
					->when($param['nav_id'], function($query) use ($param){
						$param['nav_id'] = is_array($param['nav_id']) ?: [$param['nav_id']];
						$query->where('course.nav_id', 'in', $param['nav_id']);
					})
					->where('user_id', 'neq', 0)
					->join('know_user','know_user.know_user_id=ques.user_id')
					->join('course','course.course_id=ques.course_id')
					->join('nav','nav.nav_id=ques.nav_id')
					->join('type','type.type_id=ques.type_id')
					->join('specific','specific.s_id=ques.s_id')
					->order('answer asc,time desc')
					->paginate(20,false,['query'=>$query]);
	}

	public function getOneData($param)
	{
		return self::alias('ques')
					->field('answer,id,content,ques.course_id,user_id,nav_name,type_name,s_name,course_name,know_nickname,know_phone,ques.nav_id,ques.type_id,ques.s_id')
					->where('id',$param['id'])
					->join('know_user','know_user.know_user_id=ques.user_id')
					->join('course','course.course_id=ques.course_id')
					->join('type','ques.type_id=type.type_id')
					->join('specific','ques.s_id=specific.s_id')
					->join('nav','ques.nav_id=nav.nav_id')
					->find();
	}
}